package com.jade.store.wechat.biz.protocol;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2016/12/16
 * Time: 19:26
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
public class WechatAppAutoPush {
    // 开发者微信号
    private String ToUserName;
    // 	发送方帐号（一个OpenID）
    private String FromUserName;

    // 消息创建时间 （整型）
    private String CreateTime;
//消息类型，event
    private String MsgType;
    // 事件类型，SCAN
    private String Event;
    //事件KEY值，是一个32位无符号整数，即创建二维码时的二维码scene_id
    private String EventKey;
    //二维码的ticket，可用来换取二维码图片
    private String Ticket;
    //	地理位置纬度
    private String Latitude;
    //地理位置经度
    private String Longitude;
    //	地理位置精度
    private String Precision;
    //	菜单编号
    private String MenuId;
    //	文本消息内容
    private String Content;

    //	消息id，64位整型
    private String MsgId;

    //		图片链接
    private String PicUrl;
    //		图片消息媒体id，可以调用多媒体文件下载接口拉取数据。
    private String MediaId;

    //			语音格式，如amr，speex等
    private String Format;

    //	视频消息缩略图的媒体id，可以调用多媒体文件下载接口拉取数据。
    private String ThumbMediaId;

    //		地理位置维度
    private String Location_X;
    //			地理位置经度
    private String Location_Y;
    //			地图缩放大小
    private String Scale;
    //			地理位置信息
    private String Label;

    //			消息标题
    private String Title;
    //			消息描述
    private String Description;
    //			消息链接
    private String Url;


    public String getTicket() {
        return Ticket;
    }

    public void setTicket(String ticket) {
        Ticket = ticket;
    }

    public String getLatitude() {
        return Latitude;
    }

    public void setLatitude(String latitude) {
        Latitude = latitude;
    }

    public String getLongitude() {
        return Longitude;
    }

    public void setLongitude(String longitude) {
        Longitude = longitude;
    }

    public String getPrecision() {
        return Precision;
    }

    public void setPrecision(String precision) {
        Precision = precision;
    }

    // 获取其对应的xml数据串
    public String getXML() {
        XStream xStream = new XStream(new DomDriver());
        xStream.alias("xml", this.getClass());
        // 设置成员的别名
        xStream.aliasField("AppId", this.getClass(), "AppId");
        xStream.aliasField("CreateTime", this.getClass(), "CreateTime");
        xStream.aliasField("InfoType", this.getClass(), "InfoType");
        return xStream.toXML(this);
    }

    /**
     * http://mp.weixin.qq.com/wiki/2/5baf56ce4947d35003b86a9805634b1e.html#.E5.85.B3.E6.B3.A8.2F.E5.8F.96.E6.B6.88.E5.85.B3.E6.B3.A8.E4.BA.8B.E4.BB.B6
     *
     * @param xml
     * @return
     */
    public WechatAppAutoPush getContent(String xml) {
        XStream xStream = new XStream(new DomDriver());
        xStream.alias("xml", this.getClass());
        xStream.aliasField("ToUserName", this.getClass(), "ToUserName");
        xStream.aliasField("FromUserName", this.getClass(), "FromUserName");
        xStream.aliasField("MsgType", this.getClass(), "MsgType");
        WechatAppAutoPush temp = (WechatAppAutoPush) xStream.fromXML(xml);

        if(temp.getMsgType().equals("event")){//事件性推送
            XStream eventxStream = new XStream(new DomDriver());
            eventxStream.alias("xml", this.getClass());
            eventxStream.aliasField("ToUserName", this.getClass(), "ToUserName");
            eventxStream.aliasField("FromUserName", this.getClass(), "FromUserName");
            eventxStream.aliasField("MsgType", this.getClass(), "MsgType");
            WechatAppAutoPush eventTemp = (WechatAppAutoPush) xStream.fromXML(xml);

            XStream newStream = new XStream(new DomDriver());
            newStream.alias("xml", this.getClass());
            newStream.aliasField("ToUserName", this.getClass(), "ToUserName");
            newStream.aliasField("FromUserName", this.getClass(), "FromUserName");
            newStream.aliasField("Event", this.getClass(), "Event");
            newStream.aliasField("MsgType", this.getClass(), "MsgType");
            if(eventTemp.getEvent().equals("subscribe")){//订阅
                newStream.aliasField("EventKey", this.getClass(), "EventKey");
                newStream.aliasField("Ticket", this.getClass(), "Ticket");
            }else if(eventTemp.getEvent().equals("SCAN")){//用户已关注时的事件推送
                newStream.aliasField("EventKey", this.getClass(), "EventKey");
                newStream.aliasField("Ticket", this.getClass(), "Ticket");
            }else if(eventTemp.getEvent().equals("LOCATION")){//上报地理位置事件
                newStream.aliasField("Latitude", this.getClass(), "Latitude");
                newStream.aliasField("Longitude", this.getClass(), "Longitude");
                newStream.aliasField("Precision", this.getClass(), "Precision");
            }else if(eventTemp.getEvent().equals("CLICK")){//点击菜单拉取消息时的事件推送
                newStream.aliasField("EventKey", this.getClass(), "EventKey");
            }else if(eventTemp.getEvent().equals("VIEW")){//点击菜单拉取消息时的事件推送
                newStream.aliasField("EventKey", this.getClass(), "EventKey");
                newStream.aliasField("MenuId", this.getClass(), "MenuId");
            }
            WechatAppAutoPush authPush=(WechatAppAutoPush) newStream.fromXML(xml);
            return authPush;
        }else if(temp.getMsgType().equals("text")){//微信公众号 文本性推送  text
            XStream newStream = new XStream(new DomDriver());
            newStream.alias("xml", this.getClass());
            newStream.aliasField("ToUserName", this.getClass(), "ToUserName");
            newStream.aliasField("FromUserName", this.getClass(), "FromUserName");
            newStream.aliasField("MsgType", this.getClass(), "MsgType");
            newStream.aliasField("CreateTime", this.getClass(), "CreateTime");
            newStream.aliasField("Content", this.getClass(), "Content");
            newStream.aliasField("MsgId", this.getClass(), "MsgId");
            WechatAppAutoPush authPush=(WechatAppAutoPush) newStream.fromXML(xml);
            return authPush;
        }else if(temp.getMsgType().equals("image")){//图片消息
            XStream newStream = new XStream(new DomDriver());
            newStream.alias("xml", this.getClass());
            newStream.aliasField("ToUserName", this.getClass(), "ToUserName");
            newStream.aliasField("FromUserName", this.getClass(), "FromUserName");
            newStream.aliasField("MsgType", this.getClass(), "MsgType");
            newStream.aliasField("CreateTime", this.getClass(), "CreateTime");
            newStream.aliasField("PicUrl", this.getClass(), "PicUrl");
            newStream.aliasField("MediaId", this.getClass(), "MediaId");
            newStream.aliasField("MsgId", this.getClass(), "MsgId");
            WechatAppAutoPush authPush=(WechatAppAutoPush) newStream.fromXML(xml);
            return authPush;
        }else if(temp.getMsgType().equals("voice")){//语音消息
            XStream newStream = new XStream(new DomDriver());
            newStream.alias("xml", this.getClass());
            newStream.aliasField("ToUserName", this.getClass(), "ToUserName");
            newStream.aliasField("FromUserName", this.getClass(), "FromUserName");
            newStream.aliasField("MsgType", this.getClass(), "MsgType");
            newStream.aliasField("CreateTime", this.getClass(), "CreateTime");
            newStream.aliasField("Format", this.getClass(), "Format");
            newStream.aliasField("MediaId", this.getClass(), "MediaId");
            newStream.aliasField("MsgId", this.getClass(), "MsgId");
            WechatAppAutoPush authPush=(WechatAppAutoPush) newStream.fromXML(xml);
            return authPush;
        }
        else if(temp.getMsgType().equals("video")){//视频消息
            XStream newStream = new XStream(new DomDriver());
            newStream.alias("xml", this.getClass());
            newStream.aliasField("ToUserName", this.getClass(), "ToUserName");
            newStream.aliasField("FromUserName", this.getClass(), "FromUserName");
            newStream.aliasField("MsgType", this.getClass(), "MsgType");
            newStream.aliasField("CreateTime", this.getClass(), "CreateTime");
            newStream.aliasField("ThumbMediaId", this.getClass(), "ThumbMediaId");
            newStream.aliasField("MediaId", this.getClass(), "MediaId");
            newStream.aliasField("MsgId", this.getClass(), "MsgId");
            WechatAppAutoPush authPush=(WechatAppAutoPush) newStream.fromXML(xml);
            return authPush;
        }else if(temp.getMsgType().equals("shortvideo")){//小视频消息
            XStream newStream = new XStream(new DomDriver());
            newStream.alias("xml", this.getClass());
            newStream.aliasField("ToUserName", this.getClass(), "ToUserName");
            newStream.aliasField("FromUserName", this.getClass(), "FromUserName");
            newStream.aliasField("MsgType", this.getClass(), "MsgType");
            newStream.aliasField("CreateTime", this.getClass(), "CreateTime");
            newStream.aliasField("ThumbMediaId", this.getClass(), "ThumbMediaId");
            newStream.aliasField("MediaId", this.getClass(), "MediaId");
            newStream.aliasField("MsgId", this.getClass(), "MsgId");
            WechatAppAutoPush authPush=(WechatAppAutoPush) newStream.fromXML(xml);
            return authPush;
        }else if(temp.getMsgType().equals("location")){//地理位置消息
            XStream newStream = new XStream(new DomDriver());
            newStream.alias("xml", this.getClass());
            newStream.aliasField("ToUserName", this.getClass(), "ToUserName");
            newStream.aliasField("FromUserName", this.getClass(), "FromUserName");
            newStream.aliasField("MsgType", this.getClass(), "MsgType");
            newStream.aliasField("CreateTime", this.getClass(), "CreateTime");
            newStream.aliasField("Location_X", this.getClass(), "Location_X");
            newStream.aliasField("Location_Y", this.getClass(), "Location_Y");
            newStream.aliasField("Scale", this.getClass(), "Scale");
            newStream.aliasField("Label", this.getClass(), "Label");
            newStream.aliasField("MsgId", this.getClass(), "MsgId");
            WechatAppAutoPush authPush=(WechatAppAutoPush) newStream.fromXML(xml);
            return authPush;
        }else if(temp.getMsgType().equals("link")){//链接消息
            XStream newStream = new XStream(new DomDriver());
            newStream.alias("xml", this.getClass());
            newStream.aliasField("ToUserName", this.getClass(), "ToUserName");
            newStream.aliasField("FromUserName", this.getClass(), "FromUserName");
            newStream.aliasField("CreateTime", this.getClass(), "CreateTime");
            newStream.aliasField("MsgType", this.getClass(), "MsgType");
            newStream.aliasField("Title", this.getClass(), "Title");
            newStream.aliasField("Description", this.getClass(), "Description");
            newStream.aliasField("Url", this.getClass(), "Url");
            newStream.aliasField("MsgId", this.getClass(), "MsgId");
            WechatAppAutoPush authPush=(WechatAppAutoPush) newStream.fromXML(xml);
            return authPush;
        }
        return new WechatAppAutoPush();
    }

    public String getToUserName() {
        return ToUserName;
    }

    public void setToUserName(String toUserName) {
        ToUserName = toUserName;
    }

    public String getFromUserName() {
        return FromUserName;
    }

    public void setFromUserName(String fromUserName) {
        FromUserName = fromUserName;
    }

    public String getCreateTime() {
        return CreateTime;
    }

    public void setCreateTime(String createTime) {
        CreateTime = createTime;
    }

    public String getMsgType() {
        return MsgType;
    }

    public void setMsgType(String msgType) {
        MsgType = msgType;
    }

    public String getEvent() {
        return Event;
    }

    public void setEvent(String event) {
        Event = event;
    }

    public String getEventKey() {
        return EventKey;
    }

    public void setEventKey(String eventKey) {
        EventKey = eventKey;
    }

    public String getContent() {
        return Content;
    }

    public void setContent(String content) {
        Content = content;
    }

    public String getMsgId() {
        return MsgId;
    }

    public void setMsgId(String msgId) {
        MsgId = msgId;
    }

    public String getPicUrl() {
        return PicUrl;
    }

    public void setPicUrl(String picUrl) {
        PicUrl = picUrl;
    }

    public String getMediaId() {
        return MediaId;
    }

    public void setMediaId(String mediaId) {
        MediaId = mediaId;
    }

    public String getFormat() {
        return Format;
    }

    public void setFormat(String format) {
        Format = format;
    }

    public String getThumbMediaId() {
        return ThumbMediaId;
    }

    public void setThumbMediaId(String thumbMediaId) {
        ThumbMediaId = thumbMediaId;
    }

    public String getLocation_X() {
        return Location_X;
    }

    public void setLocation_X(String location_X) {
        Location_X = location_X;
    }

    public String getLocation_Y() {
        return Location_Y;
    }

    public void setLocation_Y(String location_Y) {
        Location_Y = location_Y;
    }

    public String getScale() {
        return Scale;
    }

    public void setScale(String scale) {
        Scale = scale;
    }

    public String getLabel() {
        return Label;
    }

    public void setLabel(String label) {
        Label = label;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getUrl() {
        return Url;
    }

    public void setUrl(String url) {
        Url = url;
    }
}
